<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日程标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入日程标题"
          style="width: 180px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="daterangeScheduleTime">
        <el-date-picker
          v-model="daterangeScheduleTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="日程类型" prop="datesType">
        <el-select v-model="queryParams.datesType" clearable placeholder="请选择日程类型" style="width: 190px">
          <el-option
            v-for="dict in dict.type.schedule_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="触发时间" prop="daterangeRemindTime">
        <el-date-picker
          v-model="daterangeRemindTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="提醒类型" prop="remindType">
        <el-select v-model="queryParams.remindType" placeholder="请选择提醒类型" clearable style="width: 190px">
          <el-option
            v-for="dict in dict.type.reminder_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          :disabled="single"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['situation:calendar:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['situation:calendar:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['situation:calendar:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['situation:calendar:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="calendarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="日程标题" align="center" prop="title"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日程类型" align="center" prop="remindType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.schedule_type" :value="scope.row.datesType"/>
        </template>
      </el-table-column>
      <el-table-column label="触发时间" align="center" prop="remindTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.remindTime == null ? '尚未创建' : parseTime(scope.row.remindTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提醒类型" prop="remindType" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reminder_type"
                    :value="scope.row.remindType == null ? 3 : scope.row.remindType"/>
        </template>
      </el-table-column>
      <el-table-column label="提醒内容" align="center" prop="message" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.message == null || scope.row.message === '' ? '无' : scope.row.message }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime == null ? '尚未创建' : parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime == null ? '尚未创建' : parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
            icon="el-icon-plus"
            v-hasPermi="['situation:calendar:add']"
          >添加
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['situation:calendar:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['situation:calendar:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日程提醒对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="remindType">
          <el-select v-model="form.remindType" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.reminder_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触发时间" prop="remindTime">
          <el-date-picker clearable
                          v-model="form.remindTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择触发时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提醒内容" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCalendar, getCalendar, delCalendar, addCalendar, updateCalendar} from "@/api/situation/calendar"
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "Calendar",
  dicts: ['reminder_type', 'schedule_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中的日程id
      scheduleId: '',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日程提醒表格数据
      calendarList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 关联用户的id时间范围，日程触发时间
      daterangeRemindTime: [],
      daterangeScheduleTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        datesType: null,
        remindType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remindType: [
          {required: true, message: "类型不能为空", trigger: "change"}
        ],
        remindTime: [
          {required: true, message: "触发时间不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    parseTime,
    /** 查询日程提醒列表 */
    getList() {
      this.loading = true
      if (null != this.daterangeRemindTime && '' != this.daterangeRemindTime) {
        this.queryParams.beginRemindTime = this.daterangeRemindTime[0]
        this.queryParams.endRemindTime = this.daterangeRemindTime[1]
      }
      if (this.daterangeScheduleTime != null && this.daterangeScheduleTime != '') {
        this.queryParams.beginScheduleTime = this.daterangeScheduleTime[0]
        this.queryParams.endScheduleTime = this.daterangeScheduleTime[1]
      }
      listCalendar(this.queryParams).then(response => {
        this.calendarList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        message: null,
        datesType: null,
        remindType: null,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRemindTime = []
      this.daterangeScheduleTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.scheduleId = selection.map(item => item.scheduleId)
      this.scheduleId = this.scheduleId[0]
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.scheduleId = row.scheduleId || this.scheduleId
      this.open = true
      this.title = "添加日程提醒"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      if (Array.isArray(id) && (id.length === 0 || id[0] == null)) {
        this.$message.error("请先创建提醒，在来修改")
        return false
      }
      getCalendar(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改日程提醒"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log(this.form)
            updateCalendar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.scheduleId = this.scheduleId
            addCalendar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除日程提醒编号为"' + ids + '"的数据项？').then(function () {
        return delCalendar(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('situation/calendar/export', {
        ...this.queryParams
      }, `calendar_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
